﻿using RAP.Framework.Libary.DomTemplateBuilder.Element.Conditional;
using System.Collections;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.Conditional
{
	/// <summary>
	/// 检测参数对象是否包含指定Key的Handler
	/// </summary>
	public class IsContainKeysHandler : CompareHandler
    {
        public new IsContainKeys Element
        {
            get
            {
                return base.Element as IsContainKeys;
            }
        }

		protected override bool DoCompareValue ( object propertyValue, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var paramObj = propertyValue as IDictionary;
			if ( paramObj == null )
			{
				return false;
			}
			var compareObj = this.GetCompareValue ( rootParamObject, parentParamObject, context );
			if ( compareObj == null )
			{
				return false;
			}
			var keys = new ArrayList ( paramObj.Keys );
			IEnumerator compareKeys = null;

			if ( compareObj is string )
			{
				compareKeys = compareObj.ToString ( ).Split ( ',' ).GetEnumerator ( );
			}
			else
			{
				compareKeys = ( compareObj as IList ).GetEnumerator ( );
			}
			while ( compareKeys.MoveNext ( ) )
			{
				if ( keys.IndexOf ( compareKeys.Current ) >= 0 )
				{
					return true;
				}
			}

			return false;
		}
    }
}
